﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace PIM_ATE
{
    class ClsDataRCUCheck
    {
        public int Id { get; set; }
        public int AllStatusId { get; set; }
        public string ProductSN { get; set; }
        public string Status { get; set; }
        public DateTime Time { get; set; }
        public string Remark1 { get; set; }
        public string Remark2 { get; set; }
        public string Remark3 { get; set; }

        void create()
        {
            //CREATE TABLE [tb_RCUStatusCheck] (
            //[ID] INT IDENTITY(1,1) NOT NULL,
            //[ProductSN] NVARCHAR(100) NOT NULL,
            //[Status] NVARCHAR(50) NOT NULL,
            //[time] datetime NOT NULL,
            //[remark1] NVARCHAR(100) NULL,
            //[remark2] NVARCHAR(100) NULL,
            //[remark3] NVARCHAR(100) NULL
            //)
        }
    }

    class ClsDataRCUCheckBuilder
    {
        const string TABLE_NAME = "tb_RCUStatusCheck";
        string m_sProductSN;
        string m_sConnect;
        bool m_isEnable;
        public ClsDataRCUCheckBuilder(string alias)
        {
            m_sConnect = alias;
            IniFile.SetFileName(System.Windows.Forms.Application.StartupPath + "\\JcDevices.ini");
            m_isEnable = IniFile.IniReadValue("Settings", "IsUploadData", "1") != "0";
        }

        public void construct(string sProductSN)
        {
            m_sProductSN = sProductSN;
        }

        public bool updateStatus(string sType)
        {
            if (m_sProductSN == null || m_sProductSN == "")
            {//错误
                return false;
            }

            try
            {
                DateTime timeNow = DateTime.Parse(PIMFunction.getTime(m_sConnect));
                using (var tran = MicroDBHelper.UseTransaction(IsolationLevel.RepeatableRead, m_sConnect))
                {//开始上传
                    string sSql = string.Format("select count(*) from {0} where [ProductSN] = '{1}';", TABLE_NAME, m_sProductSN);
                    int n = (int)MicroDBHelper.ExecuteScalar(sSql, null, tran);

                    if (n > 0)
                    {
                        List<SqlParameter> lstParam = new List<SqlParameter>();
                        lstParam.Add(new SqlParameter("@id", m_sProductSN));
                        lstParam.Add(new SqlParameter("@value", sType));
                        lstParam.Add(new SqlParameter("@time", timeNow));

                        sSql = "update " + TABLE_NAME + " set [Status] = @value, [time] = @time where [ProductSN] = @id;";
                        int ret = MicroDBHelper.ExecuteNonQuery(sSql, lstParam.ToArray(), tran);
                        tran.MarkSuccess();
                    }
                    else
                    {
                        string sValue = "'" + m_sProductSN + "','" + sType + "','" + timeNow.ToString("yyyy-MM-dd HH:mm:ss") + "'";
                        sSql = String.Format("insert into {0} ([ProductSN],[Status],[time]) values ({2});", TABLE_NAME, sType, sValue);
                        MicroDBHelper.ExecuteNonQuery(sSql, null, m_sConnect);
                        tran.MarkSuccess();
                    }
                }
            }
            catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); return false; }
            return true;
        }

        public bool updateStatus(ClsDataRCUCheck RCUCheck)
        {
            if (!m_isEnable || GDefine.IS_LOCAL_RESULT_ANTENNA) return true;

            try
            {
                using (var tran = MicroDBHelper.UseTransaction(IsolationLevel.RepeatableRead, m_sConnect))
                {
                    string sValue = "'" + RCUCheck.AllStatusId + "','" + RCUCheck.ProductSN + "','" + RCUCheck.Status + "','" + RCUCheck.Time.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'";
                    string sSql = String.Format("insert into {0} ([AllStatusId],[ProductSN],[Status],[Time]) values ({1}) ;", TABLE_NAME, sValue);
                    MicroDBHelper.ExecuteDataTable(sSql, null, m_sConnect);
                    tran.MarkSuccess();
                }
            }
            catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); return false; }
            return true;
        }
    }
}
